/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.gui.przedmioty;

import db.models.GrupaOcen;
import db.models.Przedmiot;
import db.models.SEMESTR;
import db.models.TYP_OCENIANIA;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import static org.gui.przedmioty.UniversalFunctions.RokAkademicki;
/**
 *
 * @author Mariushrek
 */
public class DodajPrzedmiotPanel extends javax.swing.JPanel implements KeyListener{
    private boolean czy_dodawanie_grupy_ocen = false;
    private JButton parent_OK_Button;
    private JButton OK_Button = new JButton("Dodaj do bazy");
    private JButton CANCEL_Button= new JButton("Anuluj");
    /**
     * Creates new form DodajPrzedmiot
     */
    public DodajPrzedmiotPanel(JButton k_parent_OK_Button) {
        initComponents();
        nazwa_przedmiotu.addKeyListener(this);
        parent_OK_Button = k_parent_OK_Button;
        buttonGroup1.add(sem_letni);
        buttonGroup1.add(sem_zimowy);
        sem_zimowy.setSelected(true);
        prepareGrupaOcenComboBox();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jRadioButton1 = new javax.swing.JRadioButton();
        sem_letni = new javax.swing.JRadioButton();
        buttonGroup1 = new javax.swing.ButtonGroup();
        sem_zimowy = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        typ_oceniania = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        grupa_ocen = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        nazwa_przedmiotu = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        rok_akademicki = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();

        org.openide.awt.Mnemonics.setLocalizedText(jRadioButton1, org.openide.util.NbBundle.getMessage(DodajPrzedmiotPanel.class, "DodajPrzedmiotPanel.jRadioButton1.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(sem_letni, org.openide.util.NbBundle.getMessage(DodajPrzedmiotPanel.class, "DodajPrzedmiotPanel.sem_letni.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(sem_zimowy, org.openide.util.NbBundle.getMessage(DodajPrzedmiotPanel.class, "DodajPrzedmiotPanel.sem_zimowy.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(DodajPrzedmiotPanel.class, "DodajPrzedmiotPanel.jLabel4.text")); // NOI18N

        typ_oceniania.setModel(new javax.swing.DefaultComboBoxModel(TYP_OCENIANIA.values()));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(DodajPrzedmiotPanel.class, "DodajPrzedmiotPanel.jLabel3.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle.getMessage(DodajPrzedmiotPanel.class, "DodajPrzedmiotPanel.jButton1.text")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(DodajPrzedmiotPanel.class, "DodajPrzedmiotPanel.jLabel2.text")); // NOI18N

        nazwa_przedmiotu.setText(org.openide.util.NbBundle.getMessage(DodajPrzedmiotPanel.class, "DodajPrzedmiotPanel.nazwa_przedmiotu.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(DodajPrzedmiotPanel.class, "DodajPrzedmiotPanel.jLabel5.text")); // NOI18N

        rok_akademicki.setModel(new javax.swing.DefaultComboBoxModel(RokAkademicki().toArray()));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(DodajPrzedmiotPanel.class, "DodajPrzedmiotPanel.jLabel1.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(nazwa_przedmiotu, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(grupa_ocen, javax.swing.GroupLayout.Alignment.LEADING, 0, 205, Short.MAX_VALUE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(rok_akademicki, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(sem_zimowy, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(sem_letni, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE))
                                .addComponent(typ_oceniania, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nazwa_przedmiotu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(grupa_ocen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addGap(11, 11, 11)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(typ_oceniania, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sem_zimowy)
                    .addComponent(sem_letni))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rok_akademicki, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        GrupaOcenDodajPanel panel = new GrupaOcenDodajPanel(OK_Button);
        OK_Button.setSelected(false); CANCEL_Button.setSelected(false);
        OK_Button.setEnabled(false);
        OK_Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Dobieramy sie do tego Jbuttona
                JButton btn = (JButton)e.getSource();
                // Przez dluga sciezke ojcow docieramy do JDialogu
                JDialog panel = (JDialog) btn.getParent().getParent().getParent().getParent().getParent().getParent();
                OK_Button.setSelected(true);
                panel.dispose();
            }
        });
        CANCEL_Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               // Dobieramy sie do tego Jbuttona
               JButton btn = (JButton)e.getSource();
               // Przez dluga sciezke ojcow docieramy do JDialogu
               JDialog panel = (JDialog) btn.getParent().getParent().getParent().getParent().getParent().getParent();               
               CANCEL_Button.setSelected(true);
               panel.dispose();
            }
        });
        JButton[] objects = new JButton[]{OK_Button, CANCEL_Button};
        int zwrot = JOptionPane.showOptionDialog(this, panel, "Dodaj schemat oceniania", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, objects, objects[0]);
        if(OK_Button.isSelected()) zwrot = 0;
        if(CANCEL_Button.isSelected()) zwrot = 1;
        if(zwrot == 0) {
            panel.SaveAll();
            grupa_ocen.setModel(new javax.swing.DefaultComboBoxModel(GrupaOcen.getAllGrupaOcen(0).toArray()));
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void prepareGrupaOcenComboBox() {
        List<GrupaOcen> listagrupocen = GrupaOcen.getAllGrupaOcen(0);
        for (GrupaOcen p : listagrupocen) {
            grupa_ocen.addItem(p);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox grupa_ocen;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JTextField nazwa_przedmiotu;
    private javax.swing.JComboBox rok_akademicki;
    private javax.swing.JRadioButton sem_letni;
    private javax.swing.JRadioButton sem_zimowy;
    private javax.swing.JComboBox typ_oceniania;
    // End of variables declaration//GEN-END:variables
       
   
    public boolean SaveAll() {
        String nazwa = nazwa_przedmiotu.getText();
        TYP_OCENIANIA typ = TYP_OCENIANIA.valueOf(typ_oceniania.getSelectedItem().toString());
        int grupa = ((GrupaOcen) grupa_ocen.getSelectedItem()).getId();
        SEMESTR semestr = (sem_zimowy.isSelected()? SEMESTR.ZIMOWY : SEMESTR.LETNI);
        int rok_akad = Integer.parseInt(rok_akademicki.getSelectedItem().toString().split("/")[0]);
        
        return Przedmiot.addPrzedmiot(nazwa, GrupaOcen.getGrupaOcen(grupa), typ, rok_akad, semestr);
    }
    /*
    * Metody obslugujace keyListenera do validacji wpisanych danych
    */
    private void validator() {
        if(!nazwa_przedmiotu.getText().equals("")) {
            parent_OK_Button.setEnabled(true);
        } else {
            parent_OK_Button.setEnabled(false);
        }
    }

    @Override public void keyTyped(KeyEvent ke) { validator(); }
    @Override public void keyPressed(KeyEvent ke) { validator(); }
    @Override public void keyReleased(KeyEvent ke) { validator(); }
}
